package cn.xianshu.shopping_cart.controller;


import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.shopping_cart.domain.ShoppingCart;
import cn.xianshu.shopping_cart.service.IShoppingCartService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/cart")
@Api(tags = "购物车", description = "购物车详细描述")
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;

    @PutMapping("/addcart/{id}")
    public AxiosResult addcart(@PathVariable("id") Long id){
        try {
            shoppingCartService.addcart(id);
            return AxiosResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }

    /**
     * 购物车展示
     * @return
     */
    @PostMapping("/carts")
    public AxiosResult cartdata(){
        try {
            ShoppingCart cart = shoppingCartService.cartdata();
            return AxiosResult.me().setResultObj(cart);
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AxiosResult removeOne(@PathVariable("id") Long id){
        try {
            shoppingCartService.remove(id);
            return AxiosResult.me().setMessage("删除成功");
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }

    /**
     * 金额计算
     * @param ids
     * @return
     */
    @PostMapping("/cal")
    public AxiosResult cal(@RequestBody List<Long> ids){
        try {
            List<ShoppingCart> carts = shoppingCartService.cal(ids);
            return AxiosResult.me().setResultObj(carts);
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }
}
